package com.firebird.app.kotlin.stock.ui.detailsallocatin

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.AllocationManageBean.RespDataBean.DataBean
import com.google.gson.Gson
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.data.AllocationManageBean
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.ui.detailsallocatin.contract.DetailsAllocationContract
import com.firebird.app.kotlin.stock.ui.detailsallocatin.presenter.DetailsAllocationPresenter
import com.firebird.app.kotlin.stock.ui.godeal.GoDealActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.StateUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil


class DetailsAllocationActivity : BaseMvpActivity<DetailsAllocationPresenter?>(), DetailsAllocationContract.View {
    @BindView(com.firebird.app.kotlin.stock.R.id.back)
    var back: LinearLayout? = null
    @BindView(com.firebird.app.kotlin.stock.R.id.tv_title)
    var tvTitle: TextView? = null
    @BindView(R.id.tv_id)
    var tvId: TextView? = null
    @BindView(R.id.tv_date)
    var tvDate: TextView? = null
    @BindView(R.id.tv_stockid)
    var tvStockid: TextView? = null
    @BindView(R.id.tv_state)
    var tvState: TextView? = null
    @BindView(R.id.tv_cash)
    var tvCash: TextView? = null
    @BindView(R.id.tv_all)
    var tvAll: TextView? = null
    @BindView(R.id.tv_warning)
    var tvWarning: TextView? = null
    @BindView(R.id.tv_close)
    var tvClose: TextView? = null
    @BindView(R.id.tv_service)
    var tvService: TextView? = null
    @BindView(R.id.tv_start_time)
    var tvStartTime: TextView? = null
    @BindView(R.id.tv_end_time)
    var tvEndTime: TextView? = null
    @BindView(R.id.bt_todeal)
    var btTodeal: TextView? = null
    @BindView(R.id.bt_add)
    var btAdd: TextView? = null
    @BindView(R.id.bt_supplement)
    var btSupplement: TextView? = null
    @BindView(R.id.bt_stop)
    var btStop: TextView? = null
    @BindView(R.id.iv_head)
    var ivHead: ImageView? = null
    private var strJson: String? = null
    private var strCash: String? = null
    private var strTotle: String? = null
    private var strPay: String? = null
    private var bpOrderId = 0
    private var dialog: Dialog? = null
    override val layoutId: Int
        get() = R.layout.activity_details_allocation

    override fun initView() {
        strJson = intent.extras.getString("bean")
        val gson = Gson()
        val dataBean = gson.fromJson(strJson, DataBean::class.java)
        bpOrderId = dataBean.bpOrderId
        mPresenter = DetailsAllocationPresenter()
        mPresenter.attachView(this)
        if (dataBean.bpType == 1) { //按日
            ivHead!!.setImageResource(R.mipmap.allocation_day)
        }
        if (dataBean.bpType == 2) { //按月
            ivHead!!.setImageResource(R.mipmap.allocation_mon)
        }
        if (dataBean.bpType == 3) { //免息微策略
            ivHead!!.setImageResource(R.mipmap.allocation_free)
        }
        if (dataBean.bpStatus == 1) { //操盘中
            btTodeal!!.visibility = View.VISIBLE
        } else {
            btTodeal!!.visibility = View.GONE
        }
        tvId!!.text = "操盘方案号：" + dataBean.orderNo
        tvDate!!.text = "申请时间：" + dataBean.addTime
        tvState.setText(StateUtils.getState(dataBean.bpStatus + ""))
        //保证金
        strCash = dataBean.guaranteeAmt + ""
        tvCash!!.text = strCash
        //操盘总金额
        strTotle = dataBean.totalAmt + ""
        tvAll!!.text = strTotle
        tvWarning.setText(dataBean.warningAmt + "")
        tvClose.setText(dataBean.closeAmt + "")
        tvService!!.text = dataBean.fee
        tvStartTime!!.text = dataBean.formatBeginTime
        tvEndTime!!.text = dataBean.formatEndTime
    }

    override fun showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this)
    }

    override fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
    }

    fun onSuccessAddCash(bean: SimpleBean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            val d = strPay!!.toDouble() + strCash!!.toDouble()
            tvCash!!.text = d.toString() + ""
            ToastUtil.showToast(this, bean.getRespDesc())
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this)
        } else if (bean.getRespCode().equals(Config.NOMONEY)) {
            ToastUtil.showToast(this, bean.getRespDesc())
        } else {
            ToastUtil.showToast(this, bean.getRespDesc())
        }
    }

    fun onSuccessOver(bean: SimpleBean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            finish()
            ToastUtil.showToast(this, bean.getRespDesc())
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this)
        } else if (bean.getRespCode().equals(Config.NOMONEY)) {
            ToastUtil.showToast(this, bean.getRespDesc())
        } else {
            ToastUtil.showToast(this, bean.getRespDesc())
        }
    }

    /**
     * 追加
     */
    private fun showDialogAdd() {
        val dialog = AlertDialog.Builder(this).create()
        val layoutInflater = this.layoutInflater
        val view: View = layoutInflater.inflate(R.layout.dialog_add, null)
        dialog.setView(view)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        //        WindowManager.LayoutParams params = builder.getWindow().getAttributes();
//        params.width = 800;
        val window = dialog.window
        //        window.setAttributes(params);
//去掉白色背景
        window.setBackgroundDrawableResource(R.color.transparent)
        //追加保证金
        val et_addmoney = view.findViewById<EditText>(R.id.dialog_addmoney)
        //操盘总金额
        val tv_totle = view.findViewById<TextView>(R.id.dialog_totle)
        //当前保证金
        val tv_cash = view.findViewById<TextView>(R.id.dialog_cash)
        //显示追加保证金
        val tv_addcash = view.findViewById<TextView>(R.id.dialog_addcash)
        //需要支付金额
        val tv_pay = view.findViewById<TextView>(R.id.dialog_pay)
        tv_totle.text = strTotle
        tv_cash.text = strCash
        et_addmoney.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {
                strPay = et_addmoney.text.toString().trim { it <= ' ' }
                tv_addcash.text = strPay
                tv_pay.text = "共计付款：" + strPay + "元"
            }
        })
        view.findViewById<View>(R.id.iv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_sure).setOnClickListener {
            mPresenter.getAddCashData(bpOrderId, strPay)
            dialog.dismiss()
        }
    }

    /**
     * 补亏
     */
    private fun showDialogSupplement() {
        val dialog = AlertDialog.Builder(this).create()
        val layoutInflater = this.layoutInflater
        val view: View = layoutInflater.inflate(R.layout.dialog_supplement, null)
        dialog.setView(view)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        val window = dialog.window
        window.setBackgroundDrawableResource(R.color.transparent)
        view.findViewById<View>(R.id.iv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_sure).setOnClickListener { view ->
            ToastUtil.showToast(view.context, "确认补充")
            dialog.dismiss()
        }
    }

    /**
     * 终止
     */
    private fun showDialogStop() {
        val dialog = AlertDialog.Builder(this).create()
        val layoutInflater = this.layoutInflater
        val view: View = layoutInflater.inflate(R.layout.dialog_stop, null)
        dialog.setView(view)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        val window = dialog.window
        window.setBackgroundDrawableResource(R.color.transparent)
        view.findViewById<View>(R.id.iv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_cancle).setOnClickListener { dialog.dismiss() }
        view.findViewById<View>(R.id.tv_sure).setOnClickListener {
            mPresenter.getOverDealData(bpOrderId)
            dialog.dismiss()
        }
    }

    @OnClick(R.id.back, R.id.bt_todeal, R.id.bt_add, R.id.bt_supplement, R.id.bt_stop)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.back -> finish()
            R.id.bt_todeal -> {
                val bundle = Bundle()
                bundle.putInt("orderid", bpOrderId)
                toClass(view.context, GoDealActivity::class.java, bundle)
            }
            R.id.bt_add -> showDialogAdd()
            R.id.bt_supplement -> showDialogSupplement()
            R.id.bt_stop -> showDialogStop()
        }
    }
}